/* eslint-disable max-len */
import React from 'react';
import YAML from 'yaml';
import { hashHistory } from 'react-router';
import { message } from 'antd';
import { getAPPListApi } from '@/api/Choreographer/index';

import {
  getWorkFlowsApi,
  // workFlowActions,
  addWorkFlowsApi,
  editWorkFlowsApi,
  deleteWorkFlowsApi,
  // getActionInfoApi,
  testWorkFlowsApi,
  workFlowRunInfoApi,
  uploadPlaybookApi,
  exportPlaybookApi,
  checkPlayDelete,
} from '@/api/Choreographer/play';

import { getScriptsApi } from '@/api/Choreographer/script';

const usePlayBook = () => {
  const [playList, setPlayList] = React.useState([]);
  const [appList, setAppList] = React.useState();
  const [scriptList, setScriptList] = React.useState();
  const getWorkFlows = (param) => {
    getWorkFlowsApi(param).then((res) => {
      if (res.code === 200) {
        if (res.data) {
          console.log(JSON.stringify(res.data, null, 2));
          const data = res.data.map((item) => ({
            ...item,
            definition: YAML.parse(item.definition),
            key: item.id,
          }));
          setPlayList(data);
        }
      }
    });
  };

  // const getActionInfo = (param) => {
  //   getActionInfoApi(param).then((res) => {
  //     if (res.code === 200) {
  //       setAppList(res.data);
  //     }
  //   });
  // };
  const getAppList = (param) => {
    getAPPListApi(param).then((res) => {
      if (res.code === 200 && res.data.items) {
        setAppList(res.data.items);
      }
    });
  };

  const getScriptList = (param) => {
    getScriptsApi(param).then((res) => {
      if (res.code === 200 && res.data.items) {
        const list = Object.values(res.data.items).reduce((pre, cur) => {
          if (!cur.length) return pre;
          return pre.concat(cur);
        }, []);
        setScriptList(list);
      }
    });
  };

  const addWorkFlows = (param) => {
    addWorkFlowsApi(param).then((res) => {
      if (res.code === 200 && res.status === 200) {
        message.success('新增剧本成功');
        hashHistory.push('Layout/Play/index');
      } else if (res.err_detail) {
        message.error(`${res.message} ${res.err_detail}`);
      }
    });
  };

  const editWorkFlows = (param) => {
    editWorkFlowsApi(param).then((res) => {
      if (res.code === 200 && res.status === 200) {
        message.success('编辑剧本成功');
        hashHistory.push('Layout/Play/index');
      } else if (res.err_detail) {
        message.error(`${res.message} ${res.err_detail}`);
      }
    });
  };

  const deletePlayBook = (param) => {
    checkPlayDelete(param).then((res) => {
      if (res?.data?.repeat) {
        message.success('删除失败,因其正在被调度使用中');
      } else {
        deleteWorkFlowsApi(param).then((result) => {
          if (result.code === 200 && result.status === 200) {
            message.success('删除剧本成功');
            getWorkFlows();
          } else if (result.err_detail) {
            message.error(`${result.message} ${result.err_detail}`);
          }
        });
      }
    });
  };

  const workFlowRunInfo = (param) => {
    workFlowRunInfoApi(param).then((res) => {
      console.log(res);
    });
  };

  const testWorkFlows = (param) => {
    testWorkFlowsApi(param).then((res) => {
      // console.log(res);
      setTimeout(() => {
        workFlowRunInfo({
          page: 1,
          limit: 99,
          workflow_execution_id: res.data.id,
        });
      }, 5000);
    });
  };

  // const viewWorkFlows = (param) => {
  //   getWorkFlowsApi(param).then((res) => {
  //     if (res.code === 200) {
  //       if (res.data) {
  //         console.log(res.data);
  //       }
  //     }
  //   });
  // };

  // const getWorkFlowAction = (param) => {
  //   workFlowActions(param).then((res) => {
  //     if (res.code === 200) {
  //       if (res.data) {
  //         console.log(res.data);
  //       }
  //     }
  //   });
  // };

  const uploadPlaybook = (param) => {
    console.log();
    return uploadPlaybookApi(param);
  };

  const exportPlaybook = (param) => {
    exportPlaybookApi({ workflow_ids: param.join(',') }).then((res) => {
      if (res.code === 200) {
        message.success('导出成功');
      }
    });
  };

  React.useEffect(() => {
    getWorkFlows();
    getAppList({ limit: 9999 });
    getScriptList();
    // getWorkFlowAction({ workflow_id: 'd67333e4-877b-49cf-876d-2a9384da1db8' });
    // viewWorkFlows({ identifier: 'd67333e4-877b-49cf-876d-2a9384da1db8' });
  }, []);

  return {
    playList,
    appList,
    scriptList,
    getWorkFlows,
    addWorkFlows,
    editWorkFlows,
    deletePlayBook,
    testWorkFlows,
    workFlowRunInfo,
    uploadPlaybook,
    exportPlaybook,
    // getActionInfo,
    // getWorkFlowAction,
  };
};

export default usePlayBook;
